/**
 * 
 */
package com.cvlep.mts.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import net.sf.json.JSONArray;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.StringUtils;
import org.cometd.Bayeux;
import org.cometd.Client;
import org.cometd.server.BayeuxService;

import com.cvlep.appbase.log.MTSLogger;
import com.cvlep.mts.configuration.MTSConfigurationTool;
import com.cvlep.mts.mem.UCMapping;

/**
 * @author Otupia
 *
 */
public class MTSService extends BayeuxService
{
	public static UCMapping UCMap = UCMapping.getInstance();
	public static Configuration mtsConfig = MTSConfigurationTool.getConfiguration();
	/**
	 * @param bayeux
	 * @param name
	 */
	public MTSService(Bayeux bayeux)
	{
		super(bayeux, "MessageService");
		MTSLogger.info("lalallala");
		this.subscribe(mtsConfig.getString("cometdUserPath"), "handleMemberShip");
		this.subscribe(mtsConfig.getString("cometdMessagePath"), "sendMessage");
	}

	public void handleMemberShip(Client client, Map<String, Object> data ,String messageId)
	{
		String userID = (String)data.get("userID");
		String clientID = client.getId();
		
		if(StringUtils.isNotBlank("userID") && StringUtils.isNotBlank(clientID))
		{
			//One user is allow only one logined client.
			//If try login in another client, alert the older client,
			//and replace the older one with the new client.
			
			if(UCMap.containKey(userID))
			{
				String oldClientID = UCMap.getValue(userID);
				Client oldClient = this.getBayeux().getClient(oldClientID);
				
			}
			UCMap.refreshItem(userID,clientID);
		}
		
		System.out.println(UCMap.toString());
	}
	
	/**
	 * @param client
	 * @param data
	 * data format is 
	 * {
	 * 		senderID: String,
	 * 		receiverList: String as a js Array.For Example : [Jianpu Cao,Laji Fang]
	 * 		messageBody:String
	 * }
	 * @param messageId
	 */
	
	public void sendMessage(Client client,Map<String,Object> data, String MessageID) 
	{
		String senderID = (String) data.get("senderID");
		Map receiverList = (Map) data.get("receiverList");
		//List<String> receiverList = JSONArray.toList(JSONArray.fromObject(receiverListAsJSON),String.class);
		System.out.println("data :: " + data);
		System.out.println("receiverList :: " + receiverList);
		if(StringUtils.isNotEmpty(senderID) && !receiverList.isEmpty())
		{
			//filt the message , do check the sender && receiver here
			//MessageFilter.filter(senderID,receiverList,data);
			String senderClientID = UCMap.getValue("senderID");
			Client senderClient = this.getBayeux().getClient(senderClientID);
			
			String receiverClientID = null;
			Client receiverClient = null;
			
			for(Object userID : receiverList.values())
			{
				receiverClientID = UCMap.getValue((String)userID);
				receiverClient = this.getBayeux().getClient(receiverClientID);
				receiverClient.deliver(senderClient, "/message/test", data, MessageID);
			}
		}
	}
}